package Nova.ch4.correctExtself.correct.mmcorrect.correct;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;

public class StringSelfEncoder  extends MessageToMessageEncoder<Object> {
    final static Charset UTF_8 = Charset.forName("utf-8");


    @Override
    protected void encode(ChannelHandlerContext ctx, Object o, List<Object> list) throws Exception {
        String msg=(String)o;
        ByteBuf resp = Unpooled.copiedBuffer(msg,UTF_8);
        list.add(resp);

        /*
        这一段也可以
        if (msg.length() != 0) {
            list.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), this.UTF_8));
        }*/
    }
}
